package com.code.demo.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.code.demo.R;
import com.code.demo.receiver.MyReceiverDynamic;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by yangliguo on 2019/3/3.
 */

public class MyBroadCastActivity extends Activity {

    private Unbinder unbinder;

    private MyReceiverDynamic myReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mybc);

        unbinder = ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbinder.unbind();

        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }
    }

    @OnClick(R.id.register)
    public void onRegister() {
        Log.e("TagReceiver", "onRegister");

        myReceiver = new MyReceiverDynamic();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.code.demo.MyReceiver");
        registerReceiver(myReceiver, intentFilter);
    }

    @OnClick(R.id.send)
    public void onSend() {
        Log.e("TagReceiver", "onSend");

        Intent intent = new Intent("com.code.demo.MyReceiver");
        intent.putExtra("msg", "receiver msg");
        sendBroadcast(intent);
    }
}
